package com.hzeport.bgs22emduty.config.systemLog;//package com.hzeport.config.systemLog;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;

import com.hzeport.bgs22emduty.config.interptor.ServletAttributes;
import com.hzeport.bgs22emduty.config.login.LoginCompoentService;
import com.hzeport.bgs22emduty.entity.SecurityLog;
import com.hzeport.bgs22emduty.service.SecurityLogService;
import com.hzeport.bgs22emduty.common.utils.IpUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Slf4j
@Component
public class OperatorLogger {

    @Resource
    LoginCompoentService loginComponent;
    @Resource
    SecurityLogService pSecurityLogService;

    protected Logger logger = LoggerFactory.getLogger(getClass());
//    @Pointcut("execution(* com.insigma.web..*.*(..))")
//    public void pointCut() {s
//
//    }
    @AfterReturning(value = "@annotation(OperLog)")
    public void   doLog(JoinPoint joinPoint){
//        ResultVo resultVo=new ResultVo();
        try {
            String ip="";
            String userId=null;
            String userName=null;
            String engMethod=null;
            String chsMethod=null;
            String returnValue=null;
            String paramJSON=null;
            String url=null;
            String systemName = null;
            String method = null;
            HttpServletRequest httpServletRequest=null;
            try {
                httpServletRequest= ServletAttributes.getRequest();
            }catch (Throwable throwable) {
                //这里取不到完全无所谓，甚至不需要日志
            }
            if (httpServletRequest!=null){
                ip= IpUtils.getIpAddr(httpServletRequest);
                url=httpServletRequest.getRequestURI();
                method = httpServletRequest.getMethod();
                try {
                    if (loginComponent!=null&&loginComponent.getUserInfo()!=null) {
                        userId = loginComponent.getUserInfo().getUserId();
                        userName = loginComponent.getUserInfo().getUserName();
                    }
                }catch (Exception e){
                    logger.error("用户信息未正常取得");
                }
            }
            Class clazz = joinPoint.getTarget().getClass();
            try {
                if(StringUtils.isNotEmpty(method)) {
                    if ("GET".equals(method)) {
                        paramJSON = JSONObject.toJSONString(joinPoint.getArgs());
                    }
                    paramJSON = JSONObject.toJSONString(joinPoint.getArgs());
                }
            }catch (Exception e){
                //这里取不到完全无所谓，甚至不需要日志
            }
            String methodName=joinPoint.getSignature().getName();
            Class[] parameterTypes = ((MethodSignature)joinPoint.getSignature()).getMethod().getParameterTypes();
            Method methdo = clazz.getMethod(methodName,parameterTypes);
            engMethod=methodName;
            ApiOperation annotation=methdo.getAnnotation(ApiOperation.class);
            OperLog operLogAnnotation=methdo.getAnnotation(OperLog.class);
            if (annotation != null) {
                chsMethod=annotation.value();

            }
//            returnValue=JSONObject.toJSONString(returnObject);
           // List<SysInnerOperateLogMenu> sysInnerOperateLogMenus=sysInnerOperateLogFacade.getSysInnerOperateLogMenus();
            //Set<String> needLogMenuSet=sysInnerOperateLogMenus.stream().map(SysInnerOperateLogMenu::getMethodName).collect(Collectors.toSet());
          //  if (needLogMenuSet.contains(methodName)) {
            if (operLogAnnotation != null) {
                systemName=operLogAnnotation.systemName();
                SecurityLog operateLog = new SecurityLog();
                operateLog.setCreateBy(userId);
                operateLog.setCreateTime(LocalDateTime.now());
                operateLog.setIpAddress(ip);
                operateLog.setSDescription(paramJSON);
                operateLog.setSModule(systemName);
                operateLog.setSTitle(operLogAnnotation.description());
                pSecurityLogService.save(operateLog);
            }
        } catch (Throwable throwable) {
            //☆反正都返回了，这里就算出问题，也不会怎么样的吧☆
            log.error("aop拦截方法异常",throwable);
        }
    }

}
